import java.util.LinkedList;
import java.util.List;

public class Test {
    // 链表节点的本地类
    static class Node {
        int data;
        Node next;

        // 构造函数
        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    // 链表的头节点
    Node head;

    // 向链表中添加一个新的节点
    public void add(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
    }

    // 打印链表中的所有元素
    public void printList() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " -> ");
            current = current.next;
        }
        System.out.println("null");
    }


    public static void main(String[] args) {
        int[] nums1={1,3,5,2,4};
        int[] nums2={6,5,4,3,2,1,7};
        int[] array=new int[nums1.length];
        int j=0;
        for(int i=0;i<nums1.length;i++){
            int key=findVal(nums2,nums1[i]);//返回在nums2中的下标
            if(key!=nums2.length-1){
                int max=0;
                for(int s=key+1;s<nums2.length;s++){
                    if(nums2[s]>nums2[key]){
                        array[j]=nums2[s];
                        j++;
                        max=nums2[s];
                        break;
                    }
                }
                if(max==0){
                    array[j]=-1;
                    j++;
                }

            }else{
                if(j==nums1.length-1){
                    array[j]=-1;
                }else{
                    array[j]=-1;
                    j++;
                }
            }
        }
        for (int q = 0; q <array.length; q++) {
            System.out.println(array[q]+" ");
        }
    }
    public static int findVal(int[] nums2,int val){
        for(int i=0;i<nums2.length;i++){
            if(nums2[i]==val){
                return i;
            }
        }
        return 0;
    }
    public static void main1(String[] args) {
       Node head=new Node(3);
        Node head1=new Node(0);
        Node head2=new Node(1);
        Node head3=new Node(2);
        head.next=head1;
        head1.next=head2;
        head2.next=head3;
        head3.next=head1;
        detectCycle(head);
    }
    public static Node detectCycle(Node head) {
        if(head==null){
            return null;
        }
        if(head.next==null){
            return head;
        }
        Node pp=head;
        while(pp!=null){
            Node pcur=pp.next;
            while(pcur!=null){
                if(pcur.next==pp){
                    return pp;
                }else{
                    pcur=pcur.next;
                }
            }
            pp=pp.next;
        }
        return null;
    }
}
